function rendercalss() {
    $.ajax({
        url: '/my/category/list',
        success: function (res) {
            if (res.status === 0) {
                let arr = []
                res.data.forEach(item => {
                    arr.push(`<tr>
                    <td>${item.name}</td>
                    <td>${item.alias}</td>
                    <td>
                        <button type="button" data-id='${item.id}' data-name='${item.name}' data-alias='${item.alias}' class="layui-btn layui-btn-warm layui-btn-xs editbtn">编辑</button>
                        <button type="button" id='${item.id}' class="layui-btn layui-btn-danger layui-btn-xs delbtn">删除</button>
                    </td>
                </tr>`)
                })
                $('tbody').html(arr.join(''))
            }
        }
    })
}
rendercalss()

let AI
$('.addcharge').on('click', function () {
    AI = layer.open({
        type: 1,
        title: '添加分类',
        content: $('.add-tpl').html(),
        area: ['500px', '250px']
    })
})

// 添加分类
$('body').on('submit', '#addform', function (e) {
    e.preventDefault()
    $.ajax({
        url: '/my/category/add',
        type: 'POST',
        data: $(this).serialize(),
        success: function (res) {
            if (res.status === 0) {
                layer.msg(res.message)
                rendercalss()
                layer.close(AI)
            }

        }
    })
})



let AIE
let form = layui.form
// 编辑操作
$('tbody').on('click', '.editbtn', function (e) {
    e.preventDefault()
    let data = $(this).data()
    AIE = layer.open({
        type: 1,
        title: '添加分类',
        content: $('.edit-tpl').html(),
        area: ['500px', '250px'],
        success: function () {
            // 回填
            form.val('edit', data)
        }
    })
})
// 提交
$('body').on('submit', '#editform', function (e) {
    e.preventDefault()
    $.ajax({
        url: '/my/category/update',
        type: 'POST',
        data: $(this).serialize(),
        success: function (res) {
            layer.msg(res.message)
            if (res.status === 0) {
                rendercalss()

                layer.close(AIE)

            }
        }
    })
})
// 删除图书
$('tbody').on('click', '.delbtn', function (e) {
    e.preventDefault()
    id = $(this).attr('id')
    console.log(id);

    $.ajax({
        url: '/my/category/delete',
        data: { id: id },
        success: function (res) {
            layer.msg(res.message)
            rendercalss()


        }
    })


})













